package zl.com.gz_2017029e.adapter;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import zl.com.gz_2017029e.R;
import zl.com.gz_2017029e.TopUpDialog;

/**
 * Created by Administrator on 2019/1/8 0008.
 */

public class AccountAdapter extends RecyclerView.Adapter<AccountAdapter.ViewHolder> {
    private List<Account> accountList = new ArrayList<>();
    private List<ChongZhi> chongZhiList = new ArrayList<>();
    public int chongzhi=0;
    public AccountAdapter(List<Account> accountList) {
        this.accountList = accountList;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView index;
        ImageView carlogo;
        TextView carPlate;
        TextView carOwner;
        TextView balance;
        CheckBox multi;
        Button topUp;
        LinearLayout item_layout;
        public ViewHolder(View itemView) {
            super(itemView);
            index = (TextView)itemView.findViewById(R.id.item_index);
            carPlate = (TextView)itemView.findViewById(R.id.item_carPlate);
            carOwner = (TextView)itemView.findViewById(R.id.item_carOwner);
            balance = (TextView)itemView.findViewById(R.id.item_balance);
            multi = (CheckBox)itemView.findViewById(R.id.item_multi);
            topUp = (Button)itemView.findViewById(R.id.item_top_up);
            carlogo = (ImageView)itemView.findViewById(R.id.item_logo);
            item_layout = (LinearLayout)itemView.findViewById(R.id.item_layout);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.account_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final Account account = accountList.get(position);
        holder.index.setText(String.valueOf(position+1));
        holder.carlogo.setImageResource(account.getLogoimg());
        holder.carPlate.setText(account.getLicensePlate());
        holder.carOwner.setText("车主:"+account.getCarOwner());
        holder.balance.setText("余额:"+String.valueOf(account.getBalance())+"元");
        if(account.getBalance()<10){
            holder.item_layout.setBackgroundColor(Color.parseColor("#ffcc00"));
        }else{
            holder.item_layout.setBackgroundColor(Color.WHITE);
        }
        holder.topUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(holder.multi.isChecked()){
                    TopUpDialog topUpDialog = new TopUpDialog(holder.itemView.getContext(),chongZhiList);
                    topUpDialog.setPlate(account.getLicensePlate());
//                holder.balance.setText(String.valueOf(topUpDialog.getAmount()));
                    topUpDialog.show();
                }else {
                    List<ChongZhi> nullList = new ArrayList<>();
                    TopUpDialog topUpDialog = new TopUpDialog(holder.itemView.getContext(),nullList);
                    topUpDialog.setPlate(account.getLicensePlate());
//                holder.balance.setText(String.valueOf(topUpDialog.getAmount()));
                    topUpDialog.show();
                }

            }
        });
        holder.multi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                ChongZhi chongZhi = new ChongZhi(account.getLicensePlate());
                if(b){
                    chongZhiList.add(chongZhi);
                }else {
                    for (int i=0;i<chongZhiList.size();i++){
                        if(chongZhiList.get(i).getPlate()==chongZhi.getPlate()){
                            chongZhiList.remove(i);
                        }
                    }

                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return accountList.size();
    }


}
